package com.sofwin.system.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.annotation.Log;
import com.sofwin.annotation.OperatorType;
import com.sofwin.common.ResponseEntry;
import com.sofwin.pojo.SysUser;
import com.sofwin.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @packageName: com.sofwin.system.controller
 * @user: andyliu
 * @date: 2021/12/15 10:02
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("data")
    public String data(){
        return "user/index";
    }
    @PostMapping
    @ResponseBody
    @RequiresPermissions("add:role1")
    @Log(moduleName = "用户分页信息",operatorType = OperatorType.QUERY)
    public ResponseEntry userData(SysUser user, @RequestParam(required = true,defaultValue = "1")
                                  Integer pageNumber,
                                  @RequestParam(required = true,defaultValue = "15") Integer pageSize){
        PageInfo page = userService.selectByPage(user,pageNumber,pageSize);
        return ResponseEntry.success(page);
    }
    @GetMapping("toEdit")
    public String toEdit(Integer id, Model model){
        if(id!=null){
            SysUser user = userService.selectById(id);
            model.addAttribute("user",user);
        }
        return "user/edit";
    }

    @PostMapping("save")
    @ResponseBody
    @Log(moduleName = "保存用户信息",operatorType = OperatorType.ADD)
    public ResponseEntry save(SysUser user){
        boolean status = userService.saveOrUpdate(user);
        if(status){
            return  ResponseEntry.success("保存成功");
        }else{
            return ResponseEntry.error("保存失败");
        }
    }
    @DeleteMapping("/dele/{id}")
    @ResponseBody
    @Log(moduleName = "删除用户信息",operatorType = OperatorType.DELE)
    public ResponseEntry deleteById(@PathVariable Integer id){
        boolean status = userService.deleteById(id);
        if(status){
            return  ResponseEntry.success("删除成功");
        }else{
            return ResponseEntry.error("删除失败");
        }
    }

    @GetMapping("deletes")
    @ResponseBody
    @Log(moduleName = "批量删除用户",operatorType = OperatorType.DELE)
    public ResponseEntry deletes(@RequestParam("ids[]") Integer[] ids){
        boolean status = userService.deleteByIds(ids);
        if(status){
            return  ResponseEntry.success("删除成功");
        }else{
            return ResponseEntry.error("删除失败");
        }
    }


}
